package com.home.viewmodel

import androidx.lifecycle.ViewModel
import java.util.Timer
import java.util.TimerTask
import kotlin.concurrent.timerTask

class TimeViewModel : ViewModel() {
    private lateinit var timer: Timer
    private var currentSecond: Int = 0

    fun startTiming(){
        timer = Timer()
        var timerTask:TimerTask = object: TimerTask() {
            override fun run() {
                currentSecond++
                onTimeChangeListener.let {
                    it.onTimeChange(currentSecond)
                }
            }
        }
        timer.schedule(timerTask,1000,1000)
        /*timerTask {
            currentSecond++
            onTimeChangeListener.let {
                it.onTimeChange(currentSecond)
            }
            timer.schedule(this,1000,1000)
        }*/
    }

    interface OnTimeChangeListener{
        fun onTimeChange(second:Int)
    }

    private lateinit var onTimeChangeListener:OnTimeChangeListener

    fun setOnTimeChangeListener(onTimeChangeListener: OnTimeChangeListener){
        this.onTimeChangeListener = onTimeChangeListener
    }

    override fun onCleared() {
        super.onCleared()
        timer.cancel()
    }
}